{include file="../app/common/admin/lib/header.html" title="添加单图文"}

<div class="page-header border-bottom clearfix">
  <h2 class="left">添加单图文<span>在这里添加单图文信息，左侧是预览图</span></h2>
  <a href="{:url('/appmsg/admin/index')}" class="btn btn-default btn-sm right">&lt;&nbsp;返回单图文列表</a>
</div>
<div class="addbox">
  <!--{/*<a href="{:url('appmsg/admin')}" class="btn-back">&lt;返回单图文信息列表</a>*/}-->
  <div class="addinfo clearfix">
    <!--{*<span class="ntc2">在这里添加单图文信息，左侧是预览图</span>*}-->

      <div class="showfill js_leftBox" style="position:relative;">
        <h3 class="tit js_leftTitle">{if $appmsg_info.title}{$appmsg_info.title}{else}标题{/if}</h3>
        <span class="date js_leftDate">{if $appmsg_info.add_time}{$appmsg_info.add_time|time_format}{else}{:date('Y-m-d')}{/if}</span>
        <div class="withmap js_leftCover">{if $appmsg_info.cover}<img src="{:_image($appmsg_info.cover, 'small')}" alt="封面图">{/if}</div>
        <p class="summary js_leftIntro">{if $appmsg_info.intro}{$appmsg_info.intro}{else}这里是摘要信息{/if}</p>
        <div class="single-mask" style="display:none;">
          <!--{/*<a href="javascript:void(0);" class="btn btn-default btn-xxs js_previewBtn" {if $appmsg_info}data-id="{$appmsg_info.id}"{/if}>预览图文</a>*/}-->
        </div>
      </div>

    
    <div class="fillbox js_rightBox">
      <span class="arrowbg"></span>
      <i class="arrowmayer"></i>
      <div class="innerfill">
        <div class="fillbar">
          <input type="text" value="{if $appmsg_info.title}{$appmsg_info.title}{else}标题 （必填）{/if}" class="txt1 js_title">
        </div>
        <!--{*<div class="fillbar">-->
          <!--<input type="text" value="作者 （选填）" class="txt1">-->
        <!--</div>*}-->
        <div class="fillbar" id="js_upFileBox">
          <form id="file-form" method="post" action="{$Think.const.SITE_URL}/appmsg/save/upload_file" enctype="multipart/form-data" target="upload_target">
            <a href="javascript:void(0);" class="btn-file-mayer"><i></i>上传封面</a>
            <input type="file" name="upload_cover" class="btn-file js_uploadFile">
            {if $store_id && (!$appmsg_info || ($appmsg_info && $appmsg_info.store_id == 0))}
            <span class="ntc3">大图片建议尺寸：<br/>360像素*200像素</span>
            {else}
            <input type="hidden" name="type" class="js_imageType" value="2" />
            <span class="ntc3">大图片建议尺寸：<br/>200像素*200像素</span>
            {/if}
          </form>
          <input id="hide-cover" name="cover" type="hidden" value="" />
        </div>
        <div class="fillbar hidden" id="js_upLoading">上传中...</div>
        <div class="fillbar">
          <p class="txtradio">
            <label><input type="checkbox" value="1" name="show_cover" {if $appmsg_info.show_cover == 1}checked{/if}>&nbsp;是否在详情中显示封面</label>
          </p>
        </div>
        <div class="fillbar">
          <textarea class="txt2 js_intro">{if $appmsg_info.intro}{$appmsg_info.intro}{else}摘要 （选填）{/if}</textarea>
        </div>
        <div class="fillbar">
          <p class="txtradio">
            <label><input type="radio" value="2" name="content_type" class="js_contentType" {if !$appmsg_info || $appmsg_info.content_type == 2}checked{/if}>&nbsp;图文详情</label>
            <label><input type="radio" value="1" name="content_type" class="js_contentType" {if $appmsg_info.content_type == 1}checked{/if}>&nbsp;链接地址</label>
          </p>
          <input class="txt1 js_link {if $appmsg_info.content_type != 1}hidden{/if}" type="text" value="{if $appmsg_info.link}{$appmsg_info.link}{else}链接地址（选填）{/if}">
          <textarea id="js_editor" class="txteditor js_content {if $appmsg_info.content_type == 1}hidden{/if}">{if $appmsg_info.content}{$appmsg_info.content}{else}{/if}</textarea>
        </div>
        <div class="fillbar js_sourceUrlBox" {if $appmsg_info.content_type == 1}hidden{/if}>
          <input type="text" value="{$appmsg_info.source_url}" class="txt1 js_sourceUrl">
        </div>
      </div>
    </div>
  </div>
</div>
<p class="btn-wrap2">
  <button type="button" class="btn btn-success js_saveBtn">确认</button>
  <button type="button" class="btn btn-info js_saveBtn js_noRedirect" style="margin-left:17px;">保存</button>
  <button type="button" data-id="{$appmsg_info.id}" data-is-son="{if $store_id == 0}0{else}1{/if}" class="btn btn-default js_previewBtn" style="margin-left:17px;">预览</button>
</p>
<div id="js_introData" style="display:none;">{$appmsg_info.intro}</div>

{js href='./static/js/ueditor1_4_3_3/ueditor.config.js'}
{js href='./static/js/ueditor1_4_3_3/ueditor.all.js'}
{js href="./static/js/module/appmsg.js"}
<script>
var appmsgId = "{$appmsg_info.id}";
var fillTitle = "标题 （必填）";
var fillIntro = "摘要 （选填）";
var fillLink  = "链接地址（必填）";
var fillSourceUrl  = "原文链接（选填）";

var editor;

$(function(){
  // 初始化编辑器
  editor = new UE.ui.Editor();
    //editor = new UE.getEditor("js_editor");
  editor.render("js_editor");
  
  inputPlaceHold($(".js_title"), fillTitle, "{$appmsg_info.title}");
  inputPlaceHold($(".js_intro"), fillIntro, $('#js_introData').html());
  inputPlaceHold($(".js_link"), fillLink, "{$appmsg_info.link}");
  inputPlaceHold($(".js_sourceUrl"), fillSourceUrl, "{$appmsg_info.source_url}");
  
  syncInput($(".js_title"), $(".js_leftTitle"), fillTitle, '这里是标题');
  syncInput($(".js_intro"), $(".js_leftIntro"), fillIntro, '这里是摘要');
  
  // 是输入链接还是输入图文
  $(".js_contentType").click(function(){
    var currValue = $(this).val();
    //showMsg(currValue);
    if (currValue == 1) {
      $(".js_link").removeClass('hidden');
      $(".js_content").addClass('hidden');
      $(".js_sourceUrlBox").addClass('hidden');
    } else {
      $(".js_link").addClass('hidden');
      $(".js_content").removeClass('hidden');
      $(".js_sourceUrlBox").removeClass('hidden');
    }
  });
  
  // 左侧Item hover出现
  $(".js_leftBox").live('mouseover', function(e){
    $(this).find('.single-mask').show();
  });
  // 左侧Item hover消失
  $(".js_leftBox").live('mouseout', function(e){
    $(this).find('.single-mask').hide();
  });
  
  // 上传封面
  $(".js_uploadFile").live('change', function(){
    var fileName = $(this).val();
    if (!fileName) {
      return false;
    }
    
    if (!isAllowFile(fileName)) {
      showMsg("请上传常规格式的图片,如：jpg, png等");
      return false;
    }
    
    $("#file-form").submit();
    
    $("#js_upLoading").show();
    $("#js_upFileBox").hide();
  });
  
  // 取消上传
  $(".cancelFileUpload").live('click', function(){
    $("#uploadIframe").remove();
    $(this).closest(".publish-box-pop").children("div").hide();
    $(this).closest(".publish-box-pop").children(".publish-box-pop-cont").show();
    var dateNow = new Date();
    var currTime = dateNow.getTime();
    //$("#uploadIframe").html('<iframe id="uploadIframe" name="upload_target" style="display:none;"></iframe>');
    $("#file-form").attr('target', 'upload_target'+currTime);
    $("#uploadIframebox").html('<iframe id="uploadIframe" name="upload_target'+currTime+'" style="display:none;"></iframe>');
  });
  
  // 保存图文信息
  $(".js_saveBtn").click(function(e){
    e.preventDefault();
    
    var thisObj = $(this);
    
    var title = $.trim($(".js_title").val());
    var intro = $.trim($(".js_intro").val());
    var cover = $("#hide-cover").val();
    var showCover = $(':input[name="show_cover"]:checked').length;
    var sourceUrl = $.trim($('.js_sourceUrl').val());
    if (fillSourceUrl == sourceUrl) {
      sourceUrl = '';
    }
    
    // 图文回复类型
    var contentType = $(".js_contentType:checked").val();
    var link  = $.trim($(".js_link").val());
    var content = editor.getContent();
    //alert(content);
    //return false;
    if (!title || !$.trim(title) || $.trim(title) == fillTitle || getFontNum($.trim(title)) > 64) {
      showMsg('标题不能为空且长度不能超过64字');
      return false;
    }
    if (!cover && appmsgId < 1) {
      showMsg('请上传配图');
      return false;
    }
    if (intro == fillIntro) {
      intro = '';
    }
    if ( getFontNum(intro) > 120) {
      showMsg('简介长度不能超过120字');
      return false;
    }
    if (contentType == 1) {
      if (!link || !$.trim(link) || $.trim(link) == fillLink) {
        showMsg('图文链接不能为空');
        return false;
      }
      
      if (!checkStr(link, 'link')) {
        showMsg('链接不能为空');
        return false;
      }
      
    } else if (contentType == 2) {
      if (!content || getFontNum(content) > 40000) {
        showMsg('图文详情不能为空且长度不能超过40000字');
        return false;
      }
      
      link = '';
    }
    
    if (thisObj.attr('is-post') == 1) {
      showMsg('信息提交中，请稍候...');
      return false;
    }
    thisObj.attr('is-post', 1);
    var postData = { 'id':appmsgId, 'title':title, 'intro':intro, 'cover':cover, 'content_type':contentType, 'link':link, 'content':content, 'show_cover':showCover, 'source_url':sourceUrl};
    $.post(siteUrl+'/appmsg/save/1', postData, function(json){
      if (json.info != 'ok') {
        thisObj.attr('is-post', 0);
        showMsg(json.info);
        return false;
      }
      
      thisObj.attr('is-post', 0);
      showMsg('保存成功', 1);
      
      // 确认后需跳转，仅保存时不跳转
      if (!thisObj.hasClass('js_noRedirect')) {
        if (storeId >= 1) {
          window.location.href = siteUrl+'/appmsg/admin/index?verify='+json.verify;
        } else {
          window.location.href = siteUrl+'/appmsg/admin/index';
        }
        
      } else {
        window.location.href = siteUrl+'/appmsg/add/1?id='+json.id+'&to_bottom=1';
      }
      
    }, 'json').error(function(e){
      thisObj.attr('is-post', 0);
      showMsg('网络不稳定，请稍后再试');
    });
  });
});


{if $to_bottom == 1}
setTimeout(function(){
  //console.log(1);
  $('html,body').animate({ scrollTop:$('.js_saveBtn').offset().top }, 10);
}, 800);
{/if}

// 上传后的更新
function upfileFinish(coverPath, cover, errMsg)
{
  if (!errMsg) {
    $(".js_leftCover").html('<img src="'+coverPath+'" />');
    $("#hide-cover").val(cover);
  } else {
    showMsg(errMsg);
  }
  
  $("#js_upLoading").hide();
  $(".js_uploadFile").after('<input type="file" name="upload_cover" class="btn-file js_uploadFile">');
  $(".js_uploadFile").eq(0).remove();
  $("#js_upFileBox").show();
}
</script>
<div id="uploadIframebox">
  <iframe id="uploadIframe" name="upload_target" style="display:none;"></iframe>
</div>
{include file="../app/common/admin/lib/footer.html"}